﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Collections.Generic;
using Ccit.Utils;



namespace Ccit.OA.NewsCenter
{
    public partial class sys_logo_sel : Ccit.Utils.Page
    {
        private int _pageSize = 20;
        private int _rowImgCount = 4;  //一行显示的图片数
        private const string DIR_LOGO = "images/plate_logo/";  //板块LOGO文件夹
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["pageno"] = "0";
                lnbPre.Enabled = false; //上一页按钮
                LoadLogoImage();
            }
        }
        private void LoadLogoImage()
        {
            string relPath = RequestContext.GetRelativelyPath();  // get relation path 获取相关路径(../)
            string dicPath = relPath + DIR_LOGO;  // picture dictionary path 获取图片路径(../images/plate_logo/)

            string[] picNames = FileLib.GetLogoImageName("*.gif");    //获取所有gif图片
            int[] indexArray = cmptStartEndIndex(picNames.Length);
            int startIndex = indexArray[0];
            int endIndex = indexArray[1];
            string picName = "";
            int initQuot = 0; //当前行的索引
            int iQuot = 0;
            int iRem = 0;
            List<string> lstPicName = new List<string>();
            for (int i = startIndex; i <= endIndex; i++)
            {
                picName = picNames[i];
                //记录当前列的索引,当索引为4的时候就换行
                //int result=Math.DivRem(int a,int b,out int c); 返回结果resullt=a/b ,c = a%b
                iQuot = Math.DivRem(i, _rowImgCount, out iRem);   
                if (i == startIndex)
                {
                    initQuot = iQuot;
                }

                if (i != endIndex)
                {
                    if (iQuot == initQuot)
                    {
                        lstPicName.Add(picName);
                    }
                    else    //当lstPicName.Count = 4 时,创建行
                    {
                        createTableRow(lstPicName, initQuot, dicPath);  //创建行
                        initQuot = iQuot;
                        lstPicName.Clear();
                        lstPicName.Add(picName);
                    }
                }
                else
                {
                    if (lstPicName.Count == 4)
                    {
                        createTableRow(lstPicName, initQuot, dicPath);
                        lstPicName.Clear();
                    }

                    lstPicName.Add(picName);
                    /* 计算当前行数 */
                    if (lstPicName.Count == 1)
                        initQuot++;
                    createTableRow(lstPicName, initQuot, dicPath);
                }

            }

            /* 创建空白行 */
            createEmptyRow(initQuot);

        }

        private void createTableRow(List<string> lstPicName, int rowIndex, string dicPath)
        {
            int icount = lstPicName.Count;
            HtmlTableRow row = new HtmlTableRow();
            row.ID = "row_" + rowIndex;
            HtmlTableCell cell;
            string picName = "";
            string dealPicName = "";
            for (int i = 0; i < icount; i++)
            {
                picName = lstPicName[i];
                dealPicName = getPicName(picName);
                cell = createTableCell(dealPicName, i, rowIndex, dicPath);
                row.Cells.Add(cell);
            }

            /* 创建空白列 */
            for (int ii = icount; ii < 4; ii++)
            {
                cell = createEmptyCell(ii);
                row.Cells.Add(cell);
            }

            tblImage.Rows.Add(row);
        }

        private string getPicName(string fullPicName)
        {
            string singlePicName = "";

            if (string.IsNullOrEmpty(fullPicName)) return singlePicName;

            int iIndex = fullPicName.LastIndexOf("\\");
            if (iIndex != -1)
            {
                singlePicName = fullPicName.Substring((iIndex + 1));
            }
            else
            {
                iIndex = fullPicName.LastIndexOf("/");
                if (iIndex != -1)
                    singlePicName = fullPicName.Substring((iIndex + 1));
            }

            return singlePicName;
        }


        private HtmlTableCell createTableCell(string picName, int cellIndex, int rowIndex, string dicPath)
        {
            HtmlTableCell cell = new HtmlTableCell();
            const string rdName = "rdImage";
            HtmlImage image;
            HtmlInputRadioButton radio;

            /* 设置cell样式 */
            cell.Width = "25%";
            cell.Align = "center";
            string cellStyle = (cellIndex == 3) ? "pformright" : "pformrightline";
            cell.Attributes["class"] = cellStyle;

            radio = new HtmlInputRadioButton();
            radio.Name = rdName;
            radio.Attributes["class"] = "radio";
            radio.Attributes["onclick"] = "javascript:doRadioClick('img_" + rowIndex + "' + '_" + cellIndex + "')";
            cell.Controls.Add(radio);

            image = new HtmlImage();
            image.ID = "img_" + rowIndex + "_" + cellIndex;
            image.Src = dicPath + picName;
            cell.Controls.Add(image);
            return cell;
        }


        private void createEmptyRow(int createdRowCount)
        {
            HtmlTableRow row;
            HtmlTableCell cell;

            int iRowCount = (createdRowCount < 4) ? 4 - createdRowCount : 0;
            for (int ii = 0; ii < iRowCount; ii++)
            {
                row = new HtmlTableRow();
                for (int iii = 0; iii < 4; iii++)
                {
                    cell = createEmptyCell(iii);
                    row.Cells.Add(cell);
                }
                tblImage.Rows.Add(row);
            }
        }

        private HtmlTableCell createEmptyCell(int cellIndex)
        {
            HtmlTableCell cell = new HtmlTableCell();
            cell.Width = "25%";
            cell.Align = "center";
            string cellStyle = (cellIndex == 3) ? "pformright" : "pformrightline";
            cell.Attributes["class"] = cellStyle;

            cell.InnerHtml = "&nbsp;";
            return cell;
        }


        /// <summary>
        /// 计算分页起始，结束索引
        /// </summary>
        /// <param name="picLen"></param>
        /// <returns></returns>
        private int[] cmptStartEndIndex(int picLen)
        {
            int[] returnArray = new int[2];
            int startIndex = Int32.Parse(ViewState["pageno"].ToString()) * _pageSize;
            int endIndex = (Int32.Parse(ViewState["pageno"].ToString()) + 1) * _pageSize - 1;
            if (endIndex > picLen)
                endIndex = picLen - 1;
            returnArray[0] = startIndex;
            returnArray[1] = endIndex;

            /* 计算最大页数 */
            ViewState["maxpageno"] = picLen / _pageSize;
            return returnArray;
        }

        //上一页
        protected void lnbPre_Click(object sender, EventArgs e)
        {
            int iCurPageIndex = Int32.Parse(ViewState["pageno"].ToString());
            ViewState["pageno"] = (iCurPageIndex - 1).ToString();
            lnbPre.Enabled = (Int32.Parse(ViewState["pageno"].ToString()) == 0) ? false : true;
            lnbNext.Enabled = true;
            LoadLogoImage();
        }
        //下一页
        protected void lnbNext_Click(object sender, EventArgs e)
        {
            int iCurPageIndex = Int32.Parse(ViewState["pageno"].ToString());
            ViewState["pageno"] = (iCurPageIndex + 1).ToString();
            lnbNext.Enabled = (ViewState["pageno"].ToString() == ViewState["maxpageno"].ToString()) ? false : true;
            lnbPre.Enabled = true;
            LoadLogoImage();
        }
    }
}
